package cn.wenhe9.novel.service.impl;
import java.time.LocalDateTime;

import cn.wenhe9.novel.constant.ResultCodeEnum;
import cn.wenhe9.novel.domain.dto.AuthorCodeDto;
import cn.wenhe9.novel.domain.dto.AuthorIncomeDetailDto;
import cn.wenhe9.novel.domain.dto.AuthorIncomeDto;
import cn.wenhe9.novel.domain.dto.AuthorInfoDto;
import cn.wenhe9.novel.domain.entity.author.AuthorIncome;
import cn.wenhe9.novel.domain.entity.author.AuthorIncomeDetail;
import cn.wenhe9.novel.domain.entity.author.AuthorInfo;
import cn.wenhe9.novel.domain.vo.AuthorRegisterVo;
import cn.wenhe9.novel.exception.NovelException;
import cn.wenhe9.novel.mapper.AuthorCodeMapper;
import cn.wenhe9.novel.mapper.AuthorIncomeDetailMapper;
import cn.wenhe9.novel.mapper.AuthorIncomeMapper;
import cn.wenhe9.novel.mapper.AuthorInfoMapper;
import cn.wenhe9.novel.result.PageResultDto;
import cn.wenhe9.novel.service.AuthorService;
import com.alibaba.druid.sql.visitor.functions.Lpad;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作家模块 服务类
 * @author DuJinliang
 * 2022/6/1
 */
@Service
public class AuthorServiceImpl implements AuthorService {

    @Resource
    private AuthorInfoMapper authorInfoMapper;

    @Resource
    private AuthorIncomeMapper authorIncomeMapper;

    @Resource
    private AuthorIncomeDetailMapper authorIncomeDetailMapper;

    @Resource
    private AuthorCodeMapper authorCodeMapper;

    @Override
    public Boolean checkPenName(String penName) {
        LambdaQueryWrapper<AuthorInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AuthorInfo::getPenName, penName);

        Integer count = authorInfoMapper.selectCount(queryWrapper);

        return count <= 0;
    }

    @Override
    public void register(Long userId, AuthorRegisterVo vo) {
        Boolean hasPenName = this.checkPenName(vo.getPenName());

        //TODO 判断邀请码的合法时间

        if(!hasPenName){
            throw new NovelException(ResultCodeEnum.AUTHOR_PEN_NAME_EXIST);
        }

        AuthorInfo authorInfo = new AuthorInfo();
        authorInfo.setUserId(userId);
        authorInfo.setInviteCode("123");
        authorInfo.setPenName(vo.getPenName());
        authorInfo.setTelPhone(vo.getTelPhone());
        authorInfo.setChatAccount(vo.getChatAccount());
        authorInfo.setEmail(vo.getEmail());
        authorInfo.setWorkDirection(vo.getWorkDirection());
        authorInfo.setStatus(0);
        authorInfo.setCreateTime(LocalDateTime.now());
        authorInfo.setUpdateTime(LocalDateTime.now());

        //TODO 逻辑删除邀请码

        authorInfoMapper.insert(authorInfo);
    }

    @Override
    public AuthorInfo queryAuthorById(Long authorId) {
        return authorInfoMapper.selectById(authorId);
    }

    @Override
    public PageResultDto<AuthorIncomeDto> listIncomeMonthByPage(Long page, Long pageSize, Long authorId) {
        LambdaQueryWrapper<AuthorIncome> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AuthorIncome::getAuthorId, authorId);
        Page<AuthorIncome> authorIncomePage = authorIncomeMapper.selectPage(new Page<>(page, pageSize), queryWrapper);
        return PageResultDto.of(
                page,
                pageSize,
                authorIncomePage.getTotal(),
                authorIncomePage.getRecords().stream().map(item ->
                        AuthorIncomeDto.builder()
                                .incomeMonth(item.getIncomeMonth())
                                .preTaxIncome(item.getPreTaxIncome())
                                .afterTaxIncome(item.getAfterTaxIncome())
                                .payStatus(item.getPayStatus()).build()).collect(Collectors.toList())
        );
    }

    @Override
    public PageResultDto<AuthorIncomeDetailDto> listIncomeDailyByPage(Long page, Long pageSize, Long authorId) {
        LambdaQueryWrapper<AuthorIncomeDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AuthorIncomeDetail::getAuthorId, authorId);
        Page<AuthorIncomeDetail> authorIncomePage = authorIncomeDetailMapper.selectPage(new Page<>(page, pageSize), queryWrapper);
        return PageResultDto.of(
                page,
                pageSize,
                authorIncomePage.getTotal(),
                authorIncomePage.getRecords().stream().map(item ->
                        AuthorIncomeDetailDto.builder()
                                .incomeDate(item.getIncomeDate())
                                .incomeAccount(item.getIncomeAccount())
                                .incomeCount(item.getIncomeCount())
                                .incomeNumber(item.getIncomeNumber()).build()).collect(Collectors.toList())
        );
    }
}
